//
//  SportSkillShareController.m
//  CoolTennisBall
//
//  Created by Coollang on 2017/7/17.
//  Copyright © 2017年 CoolLang. All rights reserved.
//

#import "SportSkillShareController.h"
#import "CircleChartView.h"
#import "PNChart.h"
#import "SportDetailReportModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "UIImage+ImageFromView.h"

NSString * const kopenAppstoreY = @"https://appsto.re/cn/Y4Vl9.i";

@interface SportSkillShareController ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headIconImg;
@property (weak, nonatomic) IBOutlet UIImageView *locationImg;

@property (weak, nonatomic) IBOutlet CircleChartView *circleChartView;
@property (weak, nonatomic) IBOutlet UIView *circleSuperView;

@property (weak, nonatomic) IBOutlet UILabel *sportStyleLabel;
@property (weak, nonatomic) IBOutlet UILabel *sportDescribeLabel;

@property (weak, nonatomic) IBOutlet UILabel *serveDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *smashDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *volleyDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *sliceDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *topspinDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *flatDescriptionLabel;

@property (weak, nonatomic) IBOutlet UIView *shootBgView;
@property (weak, nonatomic) IBOutlet UIView *sportBgView;
@property (weak, nonatomic) IBOutlet UIView *sportStrongerBgView;
@property (strong, nonatomic) PNCircleChart *pnShootCirCleView;
@property (strong, nonatomic) PNCircleChart *pnSpeedCirCleView;
@property (strong, nonatomic) PNCircleChart *pnSportStrongerCirCleView;

@property (weak, nonatomic) IBOutlet UILabel *shootTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *sportTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *sportStrongerLab;

@property (weak, nonatomic) IBOutlet UILabel *nameBottomDsecLabel;


@property (strong,nonatomic)UIImage *screenShotImage;

@end

@implementation SportSkillShareController {
    BOOL _hasShowSportValues;   // 是否显示过动画
    BOOL isShowPlaySkills; // YES 显示击球风格， NO 显示击球数
    UserInforItem *_userInfoItem;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _userInfoItem = [User getLastUser].userInforItem;
    [self configureLocalization];
    [self updateUI];
}

- (void)configureLocalization {
    [self.headIconImg sd_setImageWithURL:[NSURL URLWithString:_userInfoItem.headIconPath] placeholderImage:[UIImage imageNamed:@"头像亚军"] options:SDWebImageRetryFailed|SDWebImageLowPriority];
    self.nameLabel.text = _userInfoItem.name;
    self.locationLabel.text = _userInfoItem.location;
    
    if (_userInfoItem.location.length <= 0){
        self.locationImg.image = nil;
    }else{
        self.locationImg.image = [UIImage imageNamed:@"rank_location"];
    }
    
    self.serveDescriptionLabel.text = NSLocalizedString(@"Serve", nil);
    self.smashDescriptionLabel.text = NSLocalizedString(@"Smash", nil);
    self.volleyDescriptionLabel.text = NSLocalizedString(@"Volley", nil);
    self.sliceDescriptionLabel.text = NSLocalizedString(@"Slice", nil);
    self.topspinDescriptionLabel.text = NSLocalizedString(@"Topspin", nil);
    self.flatDescriptionLabel.text = NSLocalizedString(@"Flat", nil);
    
    self.sportDescribeLabel.text = NSLocalizedString(@"Report Playing skills", nil);
    self.shootTitleLab.text = NSLocalizedString(@"Explosive Power", nil);
    self.sportTitleLab.text = NSLocalizedString(@"Endurance", nil);
    self.sportStrongerLab.text = NSLocalizedString(@"Intensity", nil);
    
    self.nameBottomDsecLabel.text = [NSString stringWithFormat:@"一%@%@一",_userInfoItem.name,NSLocalizedString(@"'s tennis skill analysis report", nil)];
    
}

- (void)updateUI {
    if (_hasShowSportValues && self.detailReportModel != nil) {
        return;
    } else if (self.detailReportModel != nil) {
        _hasShowSportValues = YES;
    }
    [self.view layoutIfNeeded];
    [self configurePNCircleView];
    [self configureCircleChartView];
    [self configureSportStyleLabel];
  
    [self performSelector:@selector(shareWithType:) withObject:nil afterDelay:0.5];
}

#pragma mark - Private Method
//配置爆发力及耐久力，运动强度
- (void)configurePNCircleView {
    if (self.pnShootCirCleView != nil) {
        [self.pnShootCirCleView removeFromSuperview];
    }
    self.pnShootCirCleView = [[PNCircleChart alloc] initWithFrame:self.shootBgView.bounds total:@1 current:@(self.detailReportModel.speedPercent) clockwise:YES shadow:YES shadowColor:[UIColor colorWithHex:0xdededf alpha:1.0]];
    
    self.pnShootCirCleView.showProgressAni = NO;
    self.pnShootCirCleView.duration = 0;
    
    self.pnShootCirCleView.chartType = PNChartFormatTypePercent;
    self.pnShootCirCleView.strokeColor = kSelectedColor;
    self.pnShootCirCleView.lineWidth = @6;
    self.pnShootCirCleView.countingLabel.textColor = kUnSelectedColor;
    self.pnShootCirCleView.countingLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.shootBgView addSubview:self.pnShootCirCleView];
    
    if (self.pnSpeedCirCleView != nil) {
        [self.pnSpeedCirCleView removeFromSuperview];
    }
    self.pnSpeedCirCleView = [[PNCircleChart alloc] initWithFrame:self.sportBgView.bounds total:@1 current:@(self.detailReportModel.durationPercent) clockwise:YES shadow:YES shadowColor:[UIColor colorWithHex:0xdededf alpha:1.0]];
    
    //self.pnSpeedCirCleView.countingLabel.animationDuration = 0.0001;
    self.pnSpeedCirCleView.chartType = PNChartFormatTypePercent;
    self.pnSpeedCirCleView.strokeColor = kOrangeColor;
    self.pnSpeedCirCleView.lineWidth = @6;
    self.pnSpeedCirCleView.countingLabel.textColor = kUnSelectedColor;
    self.pnSpeedCirCleView.duration = 0;
    self.pnSpeedCirCleView.showProgressAni = NO;
    
    self.pnSpeedCirCleView.countingLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.sportBgView addSubview:self.pnSpeedCirCleView];
    
    if (self.pnSportStrongerCirCleView != nil) {
        [self.pnSportStrongerCirCleView removeFromSuperview];
    }
    self.pnSportStrongerCirCleView = [[PNCircleChart alloc] initWithFrame:self.sportStrongerBgView.bounds total:@1 current:@(self.detailReportModel.hitPercent) clockwise:YES shadow:YES shadowColor:[UIColor colorWithHex:0xdededf alpha:1.0]];
    
    self.pnSportStrongerCirCleView.duration = 0;
    self.pnSportStrongerCirCleView.showProgressAni = NO;
    self.pnSportStrongerCirCleView.chartType = PNChartFormatTypePercent;
    self.pnSportStrongerCirCleView.strokeColor = [UIColor colorWithHex:0xf73f3d alpha:1.0];
    self.pnSportStrongerCirCleView.lineWidth = @6;
    self.pnSportStrongerCirCleView.countingLabel.textColor = kUnSelectedColor;
    self.pnSportStrongerCirCleView.countingLabel.font = [UIFont boldSystemFontOfSize:15];
    
    [self.sportStrongerBgView addSubview:self.pnSportStrongerCirCleView];
    [self.pnShootCirCleView strokeChart];
    [self.pnSpeedCirCleView strokeChart];
    [self.pnSportStrongerCirCleView strokeChart];
    
}
// 配置顶部圆形
- (void)configureCircleChartView {
    NSArray *dataArr = @[@(self.detailReportModel.faQiuCount),@(self.detailReportModel.kouShaCount),@(self.detailReportModel.jieJiCount),@(self.detailReportModel.xueQiuCount),@(self.detailReportModel.xuanQiuCount),@(self.detailReportModel.pingJiCount)];
    
    NSArray *titleArr = @[NSLocalizedString(@"Serve", nil), NSLocalizedString(@"Smash", nil), NSLocalizedString(@"Volley", nil), NSLocalizedString(@"Slice", nil),NSLocalizedString(@"Topspin", nil), NSLocalizedString(@"Flat", nil)];
    
    NSArray *colorArr = @[[UIColor colorWithHex:0x7abeea alpha:1.0],[UIColor colorWithHex:0x72deae alpha:1.0],[UIColor colorWithHex:0xf8c353 alpha:1.0],[UIColor colorWithHex:0xf88360 alpha:1.0],[UIColor colorWithHex:0xf06fa4 alpha:1.0],[UIColor colorWithHex:0xa489d6 alpha:1.0]];
    
    [self.circleChartView configureDataArray:dataArr titleArray:titleArr colorArray:colorArr isPresent:isShowPlaySkills withAnimationDur:0];
}
- (void)configureSportStyleLabel
{
    if (self.detailReportModel == nil) {
        return;
    }
    CGFloat ratio = 0.0f;
    ratio = (CGFloat)(self.detailReportModel.jieJiCount + self.detailReportModel.kouShaCount)/(self.detailReportModel.kouShaCount + self.detailReportModel.jieJiCount + self.detailReportModel.xueQiuCount + self.detailReportModel.xuanQiuCount + self.detailReportModel.pingJiCount);
    if (ratio < 0.2) {
        self.sportStyleLabel.text = NSLocalizedString(@"baseline type", nil);
    } else if (ratio > 0.4) {
        self.sportStyleLabel.text = NSLocalizedString(@"serve & volley type", nil);
    } else {
        self.sportStyleLabel.text = NSLocalizedString(@"comprehensive type", nil);
    }
}

#pragma mark - 分享的方法
- (void)shareWithType:(SSDKPlatformType)type {
    self.screenShotImage = [UIImage imageFromView:self.view];
    type = self.type;
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    NSString *title = [NSString stringWithFormat:@"%@%@  ",_userInfoItem.name,NSLocalizedString(@"'s tennis skill analysis report", nil)];

    //因为qq空间分享比较特殊所以要判断一下
    if (type == SSDKPlatformSubTypeQZone) {
        //创建分享参数
        [shareParams SSDKSetupShareParamsByText:@"酷浪网球分享"
         
                                         images:self.screenShotImage //传入要分享的图片
         
                                            url:[NSURL URLWithString:kopenAppstoreY]
         
                                          title:@"我今天的成绩哦"
         
                                           type:SSDKContentTypeText|SSDKContentTypeImage];
        
    }else if (type == SSDKPlatformTypeFacebook) {
        [shareParams SSDKSetupShareParamsByText:[title stringByAppendingString:kopenAppstoreY]
         
                                         images:self.screenShotImage //传入要分享的图片
         
                                            url:[NSURL URLWithString:@"www.coollang.com"]
         
                                          title:@"My grades today!"
         
                                           type:SSDKContentTypeImage];
    }else {
        //创建分享参数
        [shareParams SSDKSetupShareParamsByText:@"智能网球分享"
         
                                         images:self.screenShotImage //传入要分享的图片
         
                                            url:[NSURL URLWithString:@"www.coollang.com"]
         
                                          title:@"我今天的成绩哦"
         
                                           type:SSDKContentTypeImage];
    }
    
    //进行分享
    [ShareSDK share: type //传入分享的平台类型
     
         parameters:shareParams
     
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         if (state == SSDKResponseStateSuccess) {
             [MBProgressHUD showSuccessHudInView:[UIApplication sharedApplication].keyWindow message:NSLocalizedString(@"Share Successfully", "") afterDelay:1.5];
         }
         if (self.dismissBlock) {
             self.dismissBlock();
             self.dismissBlock = nil;
         }
     }];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication]setStatusBarHidden:true withAnimation:UIStatusBarAnimationFade];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
     [[UIApplication sharedApplication]setStatusBarHidden:false withAnimation:UIStatusBarAnimationFade];
}



@end
